local qingwei = fk.CreateSkill{
  name = "hanqing__qingwei",
}

Fk:loadTranslationTable{
  ["hanqing__qingwei"] = "倾危",
  [":hanqing__qingwei"] = "其他角色的出牌阶段开始时，其可将一张牌当【推心置腹】或【过河拆桥】对你使用。然后你可受到其造成的1点伤害，"..
  "改为视为对其使用之。准备阶段，你可令未对你发动过首句效果的角色各交给你至少一张牌。",

  ["#hanqing__qingwei-use"] = "倾危：是否将一张牌当【推心置腹】或【过河拆桥】对 %src 使用？",
  ["#hanqing__qingwei-reverse"] = "倾危：%dest 对你使用【%arg】，是否令其对你造成1点伤害，改为视为你对其使用？",
  ["#hanqing__qingwei-invoke"] = "倾危：是否令所有未对你发动过“倾危”效果的角色交给你牌？",
  ["#hanqing__qingwei-give"] = "倾危：请交给 %src 至少一张牌",

  ["$hanqing__qingwei1"] = "秦之有韩，如木之有蠹，腹心有疾。",
  ["$hanqing__qingwei2"] = "大其都者危其国，尊其臣者卑其主。",
  ["$hanqing__qingwei3"] = "素闻秦国有太后，未闻有秦王。",
}

qingwei:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(qingwei.name) then
      if target == player then
        return player.phase == Player.Start and
          table.find(player.room:getOtherPlayers(player, false), function (p)
            return not table.contains(player:getTableMark(qingwei.name), p.id) and #p:getHandlyIds() > 0
          end)
      else
        return target.phase == Player.Play and not target:isNude() and not target.dead and
          (target:canUseTo(Fk:cloneCard("dismantlement"), player) or
          target:canUseTo(Fk:cloneCard("sincere_treat"), player, { bypass_distances = true }))
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if target == player then
      if room:askToSkillInvoke(player, {
        skill_name = qingwei.name,
        prompt = "#hanqing__qingwei-invoke",
      }) then
        local tos = table.filter(room:getOtherPlayers(player), function (p)
          return not table.contains(player:getTableMark(qingwei.name), p.id) and #p:getHandlyIds() > 0
        end)
        event:setCostData(self, {tos = tos})
        return true
      end
    else
      local use = room:askToUseVirtualCard(target, {
        name = { "dismantlement", "sincere_treat" },
        skill_name = qingwei.name,
        prompt = "#hanqing__qingwei-use:"..player.id,
        cancelable = true,
        extra_data = {
          bypass_distances = true,
          exclusive_targets = { player.id },
        },
        card_filter = {
          n = 1,
          cards = target:getHandlyIds(),
        },
        skip = true,
      })
      if use then
        event:setCostData(self, {extra_data = use})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target == player then
      local targets = table.filter(room:getOtherPlayers(player), function (p)
        return not table.contains(player:getTableMark(qingwei.name), p.id)
      end)
      for _, p in ipairs(targets) do
        if player.dead then return end
        if not p.dead and not p:isNude() then
          local cards = room:askToCards(p, {
            min_num = 1,
            max_num = 999,
            include_equip = true,
            skill_name = qingwei.name,
            prompt = "#hanqing__qingwei-give:"..player.id,
            cancelable = false,
          })
          room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonGive, qingwei.name, nil, false, p)
        end
      end
    else
      room:addTableMark(player, qingwei.name, target.id)
      local use = event:getCostData(self).extra_data
      use.extra_data = use.extra_data or {}
      use.extra_data.hanqing__qingwei = target
      room:useCard(use)
    end
  end,
})

qingwei:addEffect(fk.TargetConfirming, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and table.contains(data.card.skillNames, qingwei.name) then
      local room = player.room
      local from = (data.extra_data or {}).hanqing__qingwei
      if from and not from.dead then
        local skill_event = room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect, false)
        return skill_event and skill_event.data.skill.name == qingwei.name and skill_event.data.who == player
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local src = (data.extra_data or {}).hanqing__qingwei
    if room:askToSkillInvoke(player, {
      skill_name = qingwei.name,
      prompt = "#hanqing__qingwei-reverse::"..src.id..":"..data.card.trueName,
    }) then
      room:damage{
        from = src,
        to = player,
        damage = 1,
        skillName = qingwei.name,
      }
      if player.dead or src.dead then return end
      data:cancelTarget(player)
      room:useVirtualCard(data.card.trueName, nil, player, src, qingwei.name)
    end
  end,
})

return qingwei
